package com.cz.zslu.time;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.UserMenu;
import com.cz.zslu.entity.ZslAuthentication;
import com.cz.zslu.entity.ZslStoreCommodity;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IUserMenuService;
import com.cz.zslu.service.IZslAuthenticationService;
import com.cz.zslu.service.IZslStoreCommodityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 定时获取数据
 *
 * @author zhanghaogao
 * @create 2018-09-18 下午12:52
 **/
@Component
@Slf4j
public class HistoryData {
    @Autowired
    private IZslStoreCommodityService iZslStoreCommodityService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IUserMenuService userMenuService;

    @Autowired
    private IZslAuthenticationService iZslAuthenticationService;


    /**
     * 更新到期商品
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void userRankingNum() {
        List<ZslStoreCommodity> list =
                iZslStoreCommodityService.selectList(new EntityWrapper<ZslStoreCommodity>().eq("state", 0).ne(
                        "undercarriage", 0).lt("end_time", new Date()));
        for (ZslStoreCommodity info : list) {
            info.setState(1);
            iZslStoreCommodityService.updateById(info);
        }


    }

    /**
     * 更新用户菜单权限
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void updateUserMenu() {

        List<SysUser> sysUserList =
                iSysUserService.selectList(new EntityWrapper<SysUser>().eq(
                        "status", 0));
        if (sysUserList != null && sysUserList.size() > 0) {
            for (int k = 0; k < sysUserList.size(); k++) {
                UserMenu info =
                        userMenuService.selectOne(new EntityWrapper<UserMenu>().eq("user_id", sysUserList.get(k).getId()));
                if (info == null) {
                    SysUser sysUser = iSysUserService.selectById(sysUserList.get(k).getId());
                    info = new UserMenu();
                    info.setUserId(sysUserList.get(k).getId());
                    info.setMenuIds("");
                    info.setUseName(sysUser.getUsername());
                    userMenuService.insert(info);
                }
            }
        }

    }


    /**
     * 更新用户会员权限
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void updateMemberState() {

        List<SysUser> sysUserList =
                iSysUserService.selectList(new EntityWrapper<SysUser>().eq(
                        "status", 0));
        if (sysUserList != null && sysUserList.size() > 0) {
            for (int i = 0; i < sysUserList.size(); i++) {
                SysUser sysUser = sysUserList.get(i);
                Date expireDate = sysUser.getEndTime();
                if (expireDate.getTime() < (new Date()).getTime()) {
                    sysUser.setState(6);
                    iSysUserService.updateById(sysUser);
                    ZslAuthentication zslAuthentication =
                            iZslAuthenticationService.selectOne(new EntityWrapper<ZslAuthentication>().eq("user_id", sysUser.getId()));
                    zslAuthentication.setState(6);
                    iZslAuthenticationService.updateById(zslAuthentication);
                }

            }
        }

    }


}



